package com.xingchen.feima.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingchen.feima.common.entity.OperLog;
import com.xingchen.feima.common.log.service.OperLogService;
import com.xingchen.feima.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "操作日志管理")
@RestController
@RequestMapping("/system/operLog")
public class OperLogController {

    @Autowired
    private OperLogService operLogService;


    @ApiOperation(value = "获取操作日志列表")
    @GetMapping("")
    public R getList(){
        List<OperLog> list = operLogService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "获取分页操作日志列表")
    @GetMapping("/{currentPage}/{pageSize}")
    public R getPageList(
            @ApiParam(name = "currentPage", value = "当前页码", required = true)
            @PathVariable Long currentPage,

            @ApiParam(name = "pageSize", value = "每页记录数", required = true)
            @PathVariable Long pageSize){

        Page<OperLog> pageObj = new Page<>(currentPage, pageSize);
        operLogService.page(pageObj, null);

        return  R.ok().data("items", pageObj);
    }

    @ApiOperation(value = "获取条件分页操作日志列表")
    @PostMapping("/{currentPage}/{pageSize}")
    public R getCondPageList(
            @ApiParam(name = "currentPage", value = "当前页码", required = true)
            @PathVariable Long currentPage,

            @ApiParam(name = "pageSize", value = "每页记录数", required = true)
            @PathVariable Long pageSize,

            @ApiParam(name = "searchConditionObj", value = "搜索条件", required = false)
            @RequestBody Object object){

        QueryWrapper<OperLog> queryWrapper = new QueryWrapper<>();

        Page<OperLog> pageObj = new Page<>(currentPage, pageSize);
        operLogService.page(pageObj, queryWrapper);

        return  R.ok().data("items", pageObj);
    }

    @ApiOperation(value = "新增操作日志")
    @PostMapping("")
    public R add(
            @ApiParam(name = "operLog", value = "操作日志对象", required = true)
            @RequestBody OperLog operLog){

        operLogService.save(operLog);
        return R.ok();
    }

    @ApiOperation(value = "根据ID查询操作日志")
    @GetMapping("/{id}")
    public R getById(
            @ApiParam(name = "id", value = "操作日志ID", required = true)
            @PathVariable Long id){

        OperLog operLog = operLogService.getById(id);
        return R.ok().data("items", operLog);
    }

    @ApiOperation(value = "根据ID修改操作日志")
    @PutMapping("")
    public R updateById(
            @ApiParam(name = "operLog", value = "操作日志对象", required = true)
            @RequestBody OperLog operLog){

        operLogService.updateById(operLog);
        return R.ok();
    }

    @ApiOperation(value = "根据ID删除操作日志")
    @DeleteMapping("/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "操作日志ID", required = true)
            @PathVariable Long id){

        operLogService.removeById(id);
        return R.ok();
    }

    @ApiOperation("根据ID列表批量删除操作日志")
    @DeleteMapping("/batchRemove")
    public R batchRemove(
            @ApiParam(name = "idList", value = "ID列表", required = true)
            @RequestBody List<Long> idList) {

        operLogService.removeByIds(idList);
        return R.ok();
    }
}

